<template>
  <div class="px-4 h-full overflow-hidden">
    <temnplate v-if="!isShowTemperature">
      <div class="flex justify-between items-center py-2">
        <a-input-search v-model:value="searchKey" class="w-64" placeholder="姓名/助记码/住院号/床位号" @search="getData" />
        <!-- <a-button type="primary" @click="open = true">导出</a-button> -->
        <BaseExport
          type="住院"
          name="待出院"
          code="000179"
          :search-params="{
            status: store.tabsKey,
            bqbh: store.areaCode,
            pyjm: searchKey
          }"
          >导出</BaseExport
        >
      </div>
      <BaseTable column-code="000179" :loading="loading" :scroll="{ x: 2400 }" :data-source="tableData" fill>
        <template #bodyCell="{ column, record, index }">
          <template v-if="column.dataIndex === 'xh'">
            {{ index + 1 }}
          </template>
          <template v-if="column.dataIndex === 'transferDate'">
            {{ record?.inpDischarge?.dischargeDate }}
          </template>
          <template v-if="column.dataIndex === 'chargeStatus'">
            <a-tag v-if="record.chargeStatus === 0" color="blue">未结账</a-tag>
            <a-tag v-if="record.chargeStatus === 1" color="green">已结账</a-tag>
            <a-tag v-if="record.chargeStatus === 2" color="red">退费中</a-tag>
            <a-tag v-if="record.chargeStatus === 3" color="orange">已挂账</a-tag>
          </template>
          <template v-if="column.dataIndex === 'action'">
            <BaseButton v-if="record.chargeStatus === 0" confirm-text="确定撤销通知出院?" type="link" size="small" danger @click="onCancelNoticeOut(record)"
              >撤销通知出院</BaseButton
            >
            <!-- 已结账才能点击清床 -->
            <a-button size="small" :disabled="record.chargeStatus !== 1 && record.chargeStatus !== 3" type="link" @click="onClearBed(record)"> 清床 </a-button>
            <a-button type="link" size="small" @click="onPrintTemp(record)">体温脉搏单</a-button>
            <TableMoreButton>
              <a-button type="link" size="small" @click="onPrintNursingRecord(record, '1')">护理记录单</a-button>
              <a-button type="link" size="small" @click="onPrintBlood(record)">血糖单</a-button>
              <a-button type="link" size="small" @click="onPrintAdv(record, '1')">长嘱单</a-button>
              <a-button type="link" size="small" @click="onPrintAdv(record, '0')">临嘱单</a-button>
            </TableMoreButton>
          </template>
        </template>
      </BaseTable>
    </temnplate>
    <template v-else>
      <div class="h-full">
        <TabTemperature :key="rowData.inplsh" :inplsh="rowData.inplsh" is-show-back @back="isShowTemperature = false" />
      </div>
    </template>
  </div>
</template>
<script setup>
import { ref } from 'vue'
import { message } from 'ant-design-vue'
import BaseTable from '@/components/BaseTable'
import BaseButton from '@/components/BaseButton'
import { post } from '@/utils/request'
import TabTemperature from '@/views/in-doctor/workstation/components/TabTemperature'
import useStore from '../../store'

const store = useStore()
const isShowTemperature = ref(false)
const rowData = ref({})
const searchKey = ref('')
const { loading, tableData, getData } = useTable({
  url: '/inp/zydj/getInpRecordList',
  immediate: true,
  pagination: false,
  formatParams: () => {
    return {
      status: store.tabsKey, //待出院状态标识
      bqbh: store.areaCode,
      pyjm: searchKey.value
    }
  }
})
const onClearBed = async (row) => {
  const { code } = await post('/inp/zyhs/clearBed', {
    bedCode: row.lastBedCode,
    inplsh: row.inplsh,
    wardId: row.lastWardId,
    inpAdvId: row?.inpDischarge?.inpAdvId
  })
  if (code !== 0) return message.error('清床失败')
  message.success('清床成功')
  getData()
  store.getStatusCount()
}
const onCancelNoticeOut = async (row) => {
  const { code, msg } = await post('inp/zyys/doCancelNoticeOfDischarge', {
    inplsh: row.inplsh
  })
  if (code !== 0) return message.error(msg || '撤销失败')
  message.success('撤销成功')
  getData()
  store.getStatusCount()
}
const onPrintTemp = (record) => {
  rowData.value = record
  isShowTemperature.value = true
}
const onPrintBlood = async (record) => {
  const { code, msg, data } = await post('/inp/Print/printBloodGlucoseTestSlips', { inplsh: record.inplsh })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
const onPrintNursingRecord = async (record) => {
  const { code, msg, data } = await post('/inp/InpNursingRecord/printInpNursingRecord', { inpLsh: record.inplsh })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
const onPrintAdv = async (record, type) => {
  const { code, msg, data } = await post('/inp/Print/printTemporaryDoctorOrders', {
    sfcybd: '1',
    inpAdvice: type,
    inplsh: record.inplsh
  })
  if (code !== 0) return message.error(msg)
  window.openExe?.rpt(data) //调用winform打印
}
</script>
